﻿class xxx
{
	public void RARExecute(RarOperations Mode, string RarFile,  // ERROR: Optional parameters aren't supported in C#
string Password)
	{
		// Description:-
		// Extract file(s) from RAR archive.
		// Parameters:-
		// Mode = Operation to perform on RAR Archive
		RarFile = "merde.rar'";
		// sPassword = Password (Optional)
		long lHandle;
		int iStatus;
		RAROpenArchiveData uRAR;
		RARHeaderData uHeader;
		string sStat;
		long Ret;

		uRAR.ArcName = RarFile;
		uRAR.CmtBuf = Strings.Space(16384);
		uRAR.CmtBufSize = 16384;

		if (Mode == OP_LIST)
		{
			uRAR.OpenMode = RAR_OM_LIST;
		}
		else
		{
			uRAR.OpenMode = RAR_OM_EXTRACT;
		}

		lHandle = RAROpenArchive(uRAR);
		if (uRAR.OpenResult != 0) OpenError(uRAR.OpenResult, RarFile); 
		if (Password != "") RARSetPassword(lHandle, Password); 

		if ((uRAR.CmtState == 1)) Interaction.MsgBox(uRAR.CmtBuf, Constants.vbApplicationModal + Constants.vbInformation, "Comment"); 

		iStatus = RARReadHeader(lHandle, uHeader);
		Show();
		while (!(iStatus != 0)) {
			sStat = Strings.Left(uHeader.FileName, Strings.InStr(1, uHeader.FileName, Constants.vbNullChar) - 1);
			switch (Mode) {
				case RarOperations.OP_EXTRACT:
					List1.AddItem("Extracting " + sStat);
					Ret = RARProcessFile(lHandle, RAR_EXTRACT, "", uHeader.FileName);
					break;
				case RarOperations.OP_TEST:
					List1.AddItem("Testing " + sStat);
					Ret = RARProcessFile(lHandle, RAR_TEST, "", uHeader.FileName);
					break;
				case RarOperations.OP_LIST:
					List1.AddItem("File: " + sStat + Constants.vbTab + Constants.vbTab + Constants.vbTab + "Size: " + uHeader.UnpSize);
					Ret = RARProcessFile(lHandle, RAR_SKIP, "", "");
					break;
			}

			if (Ret == 0)
			{
				List1.List(List1.ListCount - 1) = List1.List(List1.ListCount - 1) + Constants.vbTab + Constants.vbTab + "OK";
			}
			else
			{
				ProcessError(Ret);
			}

			iStatus = RARReadHeader(lHandle, uHeader);
			Refresh();
		}

		if (iStatus == ERAR_BAD_DATA) Erro("File header broken"); 

		RARCloseArchive(lHandle);
	}

	private void Form_Load()
	{
		string[] Vals;
		string Msg;
		if (Command == "")
		{
			Msg = "VBUNRAR.EXE.     This is a simple example of UNRAR.DLL usage" + Constants.vbCrLf + Constants.vbCrLf;
			Msg = Msg + "Syntax:" + Constants.vbCrLf;
			Msg = Msg + "VBUNRAR  X  <Archive>  <Password>     extract archive contents" + Constants.vbCrLf;
			Msg = Msg + "VBUNRAR  T  <Archive>  <Password>     test archive contents" + Constants.vbCrLf;
			Msg = Msg + "VBUNRAR  L  <Archive>  <Password>     view archive contents";
			Interaction.MsgBox(Msg, Constants.vbApplicationModal + Constants.vbInformation, "VBUnRAR");
			System.Environment.Exit(0);
		}
		Vals = Strings.Split(Command, " ");

		 // ERROR: Not supported in C#: ReDimStatement

		if (Vals(0) == "" | Vals(1) == "") Erro("Missing arguments!"); 
		switch (Strings.UCase(Vals(0))) {
			case "X":
				RARExecute(OP_EXTRACT, Vals(1), Vals(2));
				break;
			case "T":
				RARExecute(OP_TEST, Vals(1), Vals(2));
				break;
			case "L":
				RARExecute(OP_LIST, Vals(1), Vals(2));
				break;
			default:
				Erro("Invalid Arguments!");
				break;
		}
		Command5.Enabled = true;
	}

	private void OpenError(long ErroNum, string ArcName)
	{
		switch (ErroNum) {
			case ERAR_NO_MEMORY:
				Erro("Not enough memory");
				break;
			case ERAR_EOPEN:
				Erro("Cannot open " + ArcName);
				break;
			case ERAR_BAD_ARCHIVE:
				Erro(ArcName + " is not RAR archive");
				break;
			case ERAR_BAD_DATA:
				Erro(ArcName + ": archive header broken");
				break;
		}
	}

	private void ProcessError(long ErroNum)
	{
		switch (ErroNum) {
			case ERAR_UNKNOWN_FORMAT:
				Erro("Unknown archive format");
				break;
			case ERAR_BAD_ARCHIVE:
				Erro("Bad volume");
				break;
			case ERAR_ECREATE:
				Erro("File create error");
				break;
			case ERAR_EOPEN:
				Erro("Volume open error");
				break;
			case ERAR_ECLOSE:
				Erro("File close error");
				break;
			case ERAR_EREAD:
				Erro("Read error");
				break;
			case ERAR_EWRITE:
				Erro("Write error");
				break;
			case ERAR_BAD_DATA:
				Erro("CRC error");
				break;
		}
	}

	private void Erro(string Msg)
	{
		Interaction.MsgBox(Msg, Constants.vbApplicationModal + Constants.vbExclamation, "Error");
		System.Environment.Exit(0);
	}
}
